package org.jeecg.modules.message.enums;

import org.jeecg.common.system.annotation.EnumDict;
import org.jeecg.common.system.vo.DictModel;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用于消息数据查询【vue3】
 * 新版系统通知查询条件
 *
 * @Author taoYan
 * @Date 2022/8/19 20:41
 **/
@EnumDict("rangeDate")
public enum RangeDateEnum {

    JT("jt", "今天"),
    ZT("zt", "昨天"),
    QT("qt", "前天"),
    BZ("bz", "本周"),
    SZ("sz", "上周"),
    BY("by", "本月"),
    SY("sy", "上月"),
    ZDY("zdy", "自定义日期");

    String key;

    String title;

    RangeDateEnum(String key, String title) {
        this.key = key;
        this.title = title;
    }

    /**
     * 获取字典数据
     *
     * @return
     */
    public static List<DictModel> getDictList() {
        List<DictModel> list = new ArrayList<>();
        DictModel dictModel = null;
        for (RangeDateEnum e : RangeDateEnum.values()) {
            dictModel = new DictModel();
            dictModel.setValue(e.key);
            dictModel.setText(e.title);
            list.add(dictModel);
        }
        return list;
    }

    /**
     * 枚举（key）返回时间范围（方法重写）
     *
     * @param key 枚举 key
     * @return date[]
     * @author jiangtianyi
     * @since 1.0
     */
    public static Date[] getRangeArray(String key) {
        LocalDate begin, end;
        if (JT.key.equals(key)) {
            //今天
            begin = LocalDate.now();
            end = LocalDate.now();
        } else if (ZT.key.equals(key)) {
            //昨天
            begin = LocalDate.now().minusDays(1);
            end = LocalDate.now().minusDays(1);
        } else if (QT.key.equals(key)) {
            //前天
            begin = LocalDate.now().minusDays(2);
            end = LocalDate.now().minusDays(2);
        } else if (BZ.key.equals(key)) {
            //本周
            begin = LocalDate.now().with(WeekFields.ISO.dayOfWeek(), 1L);
            end = LocalDate.now();
        } else if (SZ.key.equals(key)) {
            //上周
            begin = LocalDate.now().minusWeeks(1).with(WeekFields.ISO.dayOfWeek(), 1L);
            end = LocalDate.now().minusWeeks(1).with(WeekFields.ISO.dayOfWeek(), 7L);
        } else if (BY.key.equals(key)) {
            //本月
            begin = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
            end = LocalDate.now();
        } else if (SY.key.equals(key)) {
            begin = LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
            end = LocalDate.now().minusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
        } else {
            return null;
        }
        // 开始时间00:00:00 结束时间23:59:59
        Date be = Date.from(LocalDateTime.of(begin, LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
        Date ed = Date.from(LocalDateTime.of(end, LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
        return new Date[]{be, ed};
    }

    public String getKey() {
        return this.key;
    }

}
